package com.rcp.login;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rcp.objects.EmployeeBean;

/**
 * Servlet implementation class DeadPageServlet
 */
@WebServlet("/DeadPageServlet")
public class DeadPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	String className = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
	String dbusername = "cs437group01";
	String dbpassword = "VHj#dj!v";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeadPageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		String pathToView = "/WEB-INF/login/DeadPage.jsp";
		
		HttpSession session = request.getSession();
		
		if(session.getAttribute("deadpage") == null) {
			
			pathToView = "/AuthenticateServlet";
			
		} else {
			
			if(request.getParameter("username") != null && request.getParameter("password") != null){
			
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			if(username.trim().length() <= 0 || password.trim().length() <= 0){
				
				pathToView = "/WEB-INF/login/DeadPage.jsp";
				
			} else {
			
					try{
						// Include the driver class
						Class.forName( className );
		
						// Establish a connection to the database
						Connection connection =
								DriverManager.getConnection(url, dbusername, dbpassword);
		
						// Create the query
						String query =	"SELECT * FROM Employees WHERE username = '" + username.toString() + "'  AND  password = '"+password.toString()+"'";
		
						// Get a reference to the statement object that will execute our query
						// on the server
						Statement statement = connection.createStatement();
		
						// Execute the query and Store the returned records (ResultSet)
						ResultSet resultSet = statement.executeQuery(query);
		
						if(resultSet == null) {
		
							pathToView = "/WEB-INF/login/DeadPage.jsp";
							
						} else {
						
							String tmpPass ="";
							String tmpUsername = "";
							int tmpStatus = 0;
							
							while ( resultSet.next() ){
			
								tmpUsername = resultSet.getString("username");					
								tmpPass = resultSet.getString("password");
								tmpStatus = resultSet.getInt("status");
			
							}
			
							// Close our DB Connection
							
							connection.close();
							
							
							if(tmpStatus == 2) {
			
								if(password.equals(tmpPass) && username.equals(tmpUsername)){
									
									session.invalidate();
									request.setAttribute("reset", true);
			
									pathToView = "/WEB-INF/login/Login.jsp";
									
								} else {
									
									pathToView = "/WEB-INF/login/DeadPage.jsp";
									
								}
								
							} else {
								
								pathToView = "/WEB-INF/login/DeadPage.jsp";
								
							}
							
						}
						
					}
					catch(SQLException e){		
						e.printStackTrace();
					}
					catch(ClassNotFoundException e){
						e.printStackTrace();
					}	
				
			}
				
			} else {
				
				pathToView = "/WEB-INF/login/DeadPage.jsp";
				
			}
					
			
		}
		
		RequestDispatcher dispatcher =
				request.getRequestDispatcher( pathToView );
			
			dispatcher.forward(request, response);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
